package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Map;

public class Menu {

    private PrintStream printStream;
    private BufferedReader bufferedReader;
    private ListBookCommand listBookCommand;
    private CheckoutBookCommand checkoutCommand;
    private ReturnBookCommand returnBookCommand;
    private Map<String, String> options;

    public Menu(PrintStream printStream, BufferedReader bufferedReader, ListBookCommand listBookCommand, CheckoutBookCommand checkoutCommand, ReturnBookCommand returnBookCommand, Map options) {
        this.printStream = printStream;
        this.bufferedReader = bufferedReader;
        this.listBookCommand = listBookCommand;
        this.checkoutCommand = checkoutCommand;
        this.returnBookCommand = returnBookCommand;
        this.options = options;
    }

//    prints in reverse order
    public void displayOptions() {
        for(String option : options.values())
            printStream.println(option);
    }

    public String run() throws IOException {
        String choice ="";
        boolean isInvalidOption = true;
        while(isInvalidOption){
            displayOptions();
            choice = bufferedReader.readLine();
            if(options.containsKey(choice)){
                isInvalidOption = false;
            }
            else
                printStream.println("Select a valid option!");
        }
        return choice;
    }

    public void executeUserChoice(String userChoice) throws IOException {
        if(userChoice.equals("1")){
            listBookCommand.listBooks();
        }
        if(userChoice.equals("2")){
            checkoutCommand.checkoutBook();
            listBookCommand.listAvailableBooks();
        }
        if(userChoice.equals("3")){
            returnBookCommand.returnBook();
        }
    }

}
